//
//  MyCustomerSubVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/19.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MyCustomerSubVc.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "OrderDM.h"
#import "NodataCell.h"
#import "OrderCell.h"
#import "TrueCustomerCell.h"




@interface MyCustomerSubVc ()
@property (nonatomic,strong)OrderDM *dm;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)NodataCell *nodataCell;

@end

@implementation MyCustomerSubVc
@synthesize state,dm,dataArray,nodataCell;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initView];
    
    
    [self refreshDataWithCache:true];
}




-(void)initView{
    
    [super initView];
    
    
    dm = [[OrderDM alloc]init];
    
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}


-(void)btnOnClick:(UIButton *)sender{
    
    
    if(sender.tag==56){
        [self refreshData];
    }
}



#pragma mark pullTableView delegate
-(void)refreshData{
    [super refreshData];
    
    [self refreshDataWithCache:false];
}



- (void)loadMore{
    [super loadMore];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
//        ResulteMod *mod = [dm loadMoreData:dm.flag];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
//            if (mod.resltState == ResultNetError) {//网络出错
//                
//                
//            }else if (mod.resltState == ResultFail){//服务器返回的code出错
//                
//                
//            }else{
//                
//                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:bself.dataArray];
//                [array addObjectsFromArray:mod.dataMod];
//                bself.dataArray = [[NSArray alloc]initWithArray:array];
//                
//            }
//            
//            [bself.tableView reloadData];
//            
//            [bself.tableView.footer endRefreshing];
            
            
        });
    });
}




-(void)refreshDataWithCache:(BOOL)cache{
    
    
    [self.tableView reloadData];
    [nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod;
//        =[dm refreshData:dm.flag WithCache:cache];
        
        dataArray = mod.dataMod;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                if(dataArray.count==0){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                    
                }else{//有数据
                    
                    [bself.nodataCell setState:nodataCellStateNorMal];
                }
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.header endRefreshing];
        });
    });
    
}





#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (dataArray.count==0) {
//        if(!nodataCell){
//            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
//            [nodataCell setState:nodataCellStateLoading];
//            [[nodataCell refreshBtn]setTag:56];
//            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
//        }
//        return nodataCell;
//    }
    
    
    
//    OrderCell *cell = [OrderCell cellWithTableView:tableView];
//    
//    [cell setData:[dataArray objectAtIndex:indexPath.row]];
    
    TrueCustomerCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"TrueCustomerCell" owner:nil options:nil]lastObject];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//    if (dataArray&&dataArray.count>0) {
//        
//        return [OrderCell getHight:nil withTableView:tableView];
//    }
//    
//    return self.tableView.frame.size.height;
    return 50;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {
        
        
    }
}

@end
